Lift Cookbook by Richard Dallaway
Author:Richard Dallaway [Richard Dallaway]
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Programming Languages / Java
ISBN: 9781449362676
Publisher: O’Reilly Media
Published: 2013-06-23T16:00:00+00:00
See Also
Custom 404 Page describes how to create a custom 404 (not found) page.
Streaming Content
Problem
You want to stream content back to the web client.
Solution
Use OutputStreamResponse, passing it a function that will write to the OutputStream that Lift supplies.
In this example, we’ll stream all the integers from one, via a REST service:
package code.rest import net.liftweb.http.{Req,OutputStreamResponse} import net.liftweb.http.rest._ object Numbers extends RestHelper { // Convert a number to a String, and then to UTF-8 bytes // to send down the output stream. def num2bytes(x: Int) = (x + "\n") getBytes("utf-8") // Generate numbers using a Scala stream: def infinite = Stream.from(1).map(num2bytes) serve { case Req("numbers" :: Nil, _, _) => OutputStreamResponse( out => infinite.foreach(out.write) ) } }
Scala’s Stream class is a way to generate a sequence with lazy evaluation. The values being produced by infinite are used as example data to stream back to the client.
Wire this into Lift in Boot.scala:
LiftRules.dispatch.append(Numbers)
Visiting http://127.0.0.1:8080/numbers will generate a 200 status code and start producing the integers from 1. The numbers are produced quite quickly, so you probably don’t want to try that in your web browser, but instead from something that is easier to stop, such as cURL.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(25283)
Hello! Python by Anthony Briggs(24336)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(23429)
Kotlin in Action by Dmitry Jemerov(22506)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(21969)
Dependency Injection in .NET by Mark Seemann(21838)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(20707)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(19521)
Grails in Action by Glen Smith Peter Ledbrook(18602)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17031)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(15840)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(13690)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(11851)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11151)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10621)
Hit Refresh by Satya Nadella(9189)
The Kubernetes Operator Framework Book by Michael Dame(8564)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8400)
Robo-Advisor with Python by Aki Ranin(8360)